﻿using System.Windows.Forms;
using CloudShot.Entities;
using NUnit.Framework;

namespace CloudShot.Tests
{
  [TestFixture]
  public class HotkeyTests
  {
    #region TryParse Method Tests

    [TestCase("")]
    [TestCase(null)]
    [TestCase(",")]
    [TestCase("|")]
    [TestCase("Ctrl+|")]
    public void TryParse_InvalidString_FalseReturned(string value)
    {
      Hotkey hotkey;

      bool result = Hotkey.TryParse(value, out hotkey);

      Assert.IsFalse(result);
      Assert.AreEqual(Hotkey.Empty, hotkey);
    }

    [Test]
    public void TryParse_StringWithPrintScreen_HotkeyParsed()
    {
      Hotkey hotkey;

      bool result = Hotkey.TryParse("Shift + PrintScreen", out hotkey);

      Assert.IsTrue(result);
      Assert.AreEqual(Keys.PrintScreen, hotkey.Key);
      Assert.AreEqual(KeyModifiers.Shift, hotkey.Modifiers);
    }

    [TestCase("Ctrl + ALT +Shift + A", Keys.A, KeyModifiers.Shift | KeyModifiers.Ctrl | KeyModifiers.Alt)]
    [TestCase("Shift +Ctrl + Win + A", Keys.A, KeyModifiers.Shift | KeyModifiers.Ctrl | KeyModifiers.Win)]
    [TestCase("Ctrl + ALT + B", Keys.B, KeyModifiers.Ctrl | KeyModifiers.Alt)]
    [TestCase("Ctrl+Shift+d", Keys.D, KeyModifiers.Ctrl | KeyModifiers.Shift)]
    [TestCase("Alt +Shift + B", Keys.B, KeyModifiers.Shift | KeyModifiers.Alt)]
    [TestCase("Control+x", Keys.X, KeyModifiers.Ctrl)]
    [TestCase("AlT + j", Keys.J, KeyModifiers.Alt)]
    [TestCase("shift +U", Keys.U, KeyModifiers.Shift)]
    [TestCase("L", Keys.L, KeyModifiers.None)]
    [TestCase("Ctrl+Alt", Keys.None, KeyModifiers.Ctrl | KeyModifiers.Alt)]
    [TestCase("Shift+Ctrl+", Keys.None, KeyModifiers.Ctrl | KeyModifiers.Shift)]
    public void TryParse_ValidHotkeyString_HotkeyParsed(string value, Keys expectedKey, KeyModifiers expectedModifiers)
    {
      Hotkey hotkey;

      bool result = Hotkey.TryParse(value, out hotkey);

      Assert.IsTrue(result);
      Assert.AreEqual(expectedKey, hotkey.Key);
      Assert.AreEqual(expectedModifiers, hotkey.Modifiers);
    }

    #endregion

    #region ToString Method Tests

    [Test]
    public void ToString_EmptyHotkey_None()
    {
      var hotkey = new Hotkey(Keys.None, KeyModifiers.None);
      string result = hotkey.ToString();

      Assert.AreEqual("None", result);
    }

    [Test]
    public void ToString_OneKey_StringWithKey()
    {
      var hotkey = new Hotkey(Keys.A);
      string result = hotkey.ToString();

      Assert.AreEqual("A", result);
    }

    [Test]
    public void ToString_ModifierAndKey_ValidString()
    {
      var hotkey = new Hotkey(Keys.A, KeyModifiers.Shift);
      string result = hotkey.ToString();

      Assert.AreEqual("Shift + A", result);
    }

    [Test]
    public void ToString_ModifiersAndKey_ValidString()
    {
      var hotkey = new Hotkey(Keys.A, KeyModifiers.Shift | KeyModifiers.Ctrl);
      string result = hotkey.ToString();

      Assert.AreEqual("Ctrl + Shift + A", result);
    }

    [Test]
    public void ToString_OnlyModifiers_ValidString()
    {
      var hotkey = new Hotkey(Keys.None, KeyModifiers.Shift | KeyModifiers.Ctrl);
      string result = hotkey.ToString();

      Assert.AreEqual("Ctrl + Shift", result);
    }

    [Test]
    public void ToString_EscapeKey_EscString()
    {
      var hotkey = new Hotkey(Keys.Escape);
      string result = hotkey.ToString();

      Assert.AreEqual("Esc", result);
    }

    #endregion
  }
}